// Install packages
ssc install ritest
net install dm79.pkg, from(https://www.stata.com/stb/stb56/)

// Read in command line arguments
args params roster output_dir output_file output_file_by_pathway

* Load local variables from the parameter file
include "`params'"

local ri_reps=5000


**************************************************
* Regressions
**************************************************
insheet using "`roster'", comma clear

* Main specification
matrix results=J(4,5,.)
matrix rownames results=i_three_components_post_20 v_shooting_or_homicide_post_20 a_shooting_or_homicide_post_20 a_partone_non_shoot_hom_post_20
local i=1
foreach k in i_three_components_post_20 v_shooting_or_homicide_post_20 a_shooting_or_homicide_post_20 a_partone_non_shoot_hom_post_20 {
	sum `k' if treatment==0
	mat results[`i',1]=r(mean)

	reg `k' `blocks' `covariates' treatment, vce(robust)
	mat results[`i',2]=_b[treatment]
	mat results[`i',3]=_se[treatment]

	test treatment
	mat results[`i',4]=r(p)

	ritest treatment _b[treatment], strata(new_blockid) seed(1234) reps(`ri_reps'): reg `k' `blocks' `covariates' treatment
	mat pval=r(p)
	mat results[`i',5]=pval[1,1]

	local i=`i'+1
}

preserve
clear
svmat2 results, names(cm itt itt_se itt_pvalue ri_pvalue) rnames(outcome)
gen outcome_id=_n
gen byte ols=1
gen byte covariates=1
gen byte blocks=1
save `output_dir', replace
restore

preserve
use `output_dir', clear
gsort +outcome_id
order outcome_id outcome ols covariates blocks
format cm* itt* ri* %15.4f
format *pvalue* %15.3f
outsheet using `output_file', comma names replace
restore

* Heterogeneity across pathway
matrix results=J(12,6,.)
local i=1
foreach k in i_three_components_post_20 v_shooting_or_homicide_post_20 a_shooting_or_homicide_post_20 a_partone_non_shoot_hom_post_20 {

	reg `k' `blocks' `covariates' c.treatment#c.(g_pathway_*), vce(robust)
	local N=e(N)

	local j=`i'
	foreach s in ul cr re {
	mat results[`i',1]=`N'

	sum `k' if treatment==0 & pathway=="`s'"
	mat results[`i',2]=r(mean)

	mat results[`i',3]=_b[c.treatment#c.g_pathway_`s']
	mat results[`i',4]=_se[c.treatment#c.g_pathway_`s']

	test c.treatment#c.g_pathway_`s'
	mat results[`i',5]=r(p)
	local i=`i'+1
	}

	ritest treatment _b[c.treatment#c.g_pathway_ul] _b[c.treatment#c.g_pathway_cr] _b[c.treatment#c.g_pathway_re], strata(new_blockid) seed(1234) reps(`ri_reps'): reg `k' `blocks' `covariates' c.treatment#c.(g_pathway_*)
	mat pval=r(p)

	local i=`j'
	local r=1
	foreach s in ul cr re {
	mat results[`i',6]=pval[1,`r']
	local i=`i'+1
	local r=`r'+1
	}
}
preserve
clear
svmat2 results, names(N cm itt itt_se itt_pvalue ri_pvalue)
gen outcome=""
gen outcome_id=.
gen pathway=""
gen pathway_id=.
local i=1
local outcome_id=1
foreach k in i_three_components_post_20 v_shooting_or_homicide_post_20 a_shooting_or_homicide_post_20 a_partone_non_shoot_hom_post_20 {
	local pathway_id=1
	foreach s in ul cr re {
		replace outcome="`k'" if _n==`i'
		replace pathway="`s'" if _n==`i'
		replace pathway_id=`pathway_id' if _n==`i'
		replace outcome_id=`outcome_id' if _n==`i'
		local i=`i'+1
		local pathway_id=`pathway_id'+1
	}
	local outcome_id=`outcome_id'+1
}
order outcome_id pathway_id outcome pathway
save `output_dir', replace
restore

preserve
use `output_dir', clear
gsort +outcome_id +pathway_id
format cm* itt* ri* %15.4f
format *pvalue* %15.3f
outsheet using `output_file_by_pathway', comma names replace
restore
